package com.xwli.business.util;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @program: springcloud
 * @description:
 * @author: xwli
 * @create: 2022-05-28 18:30
 **/
public class WordGenerateBeanTest {

    private final static Map<String, String> javaMap = new HashMap<String, String>() {{
        put("JSONString", "String");
        put("String", "String");
        put("string", "String");
        put("VarChar2", "String");
        put("Varchar2", "String");
        put("varchar", "String");
        put("int", "Integer");
        put("Int", "Integer");
        put("Integer", "Integer");
        put("Double", "Double");
        put("Long", "Long");
        put("Boolean", "Boolean");
    }};

    private final static Map<String, String> sqlMap = new HashMap<String, String>() {{
        put("String", "VARCHAR2");
        put("string", "VARCHAR2");
        put("VarChar2", "VARCHAR2");
        put("Varchar2", "VARCHAR2");
        put("varchar", "VARCHAR2");
        put("int", "integer");
        put("Int", "integer");
        put("Integer", "integer");
        put("Double", "double");
        put("double", "double");
        put("date", "TIMESTAMP");
        put("Date", "TIMESTAMP");
    }};

    private final static Integer ZI_DUAN_MING = 0;
    private final static Integer ZI_DUAN_LEI_XING = 1;
    private final static Integer ZI_DUAN_CHANG_DU = 2;
    private final static Integer ZI_DUAN_HAN_YI = 3;
    private final static Integer ZI_DUAN_BI_XU = 4;
    private final static Integer ZI_DUAN_BIE_MING_EN = 5;


    public static void main(String[] args) {
        try {
            //generateSqlSh();
            generateJavaObjCode();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @description: 生成java类+注释代码
     * @param:
     * @return: void
     * @author lixw
     * @date: 2024/11/26 14:28
     */
    private static void generateJavaObjCode() {
        List<String> obj = new LinkedList<>();
        List<List<String>> lists = ExcelUtil.readXls("C:/Users/13755/Desktop/Bean生成模板.xlsx");
        //List<List<String>> lists = ExcelUtil.readXls("C:/Users/xwli/Desktop/Bean生成模板.xlsx");
        StringBuilder stringBuilder = new StringBuilder();
        for (List<String> list : lists) {
            if (list.size() == 0)
                break;
            int i = 0;
            for (String l : list) {
                if (l.equals(""))
                    i++;
            }
            if (i == list.size())
                continue;
            String descrbe = list.get(ZI_DUAN_HAN_YI);
            descrbe = descrbe.replaceAll("\"", "");
            descrbe = descrbe.replaceAll("\n", ",");
            stringBuilder.append("/**\n* " + descrbe + "\n*/\n");
            if (list.size() > ZI_DUAN_BI_XU && StringUtils.isNotBlank(list.get(ZI_DUAN_BI_XU)) && StringUtils.equalsAny(list.get(ZI_DUAN_BI_XU), "Y", "是", "M")) {
                if ("String".equals(list.get(ZI_DUAN_LEI_XING))) {
                    stringBuilder.append("@NotBlank(message = \"" + descrbe + "不能为空\")\n");
                } else if ("Integer".equals(list.get(ZI_DUAN_LEI_XING))) {
                    stringBuilder.append("@NotNull(message = \"" + descrbe + "不能为空\")\n");
                }
            }
            if (list.size() > ZI_DUAN_BIE_MING_EN && StringUtils.isNotBlank(list.get(ZI_DUAN_BIE_MING_EN))) {
                stringBuilder.append("@XmlFieldDesc(enName = \"" + list.get(ZI_DUAN_BIE_MING_EN) + "\",chName = \"" + descrbe + "\")\n");
            }
            stringBuilder.append("private ");
            if (list.get(ZI_DUAN_LEI_XING).indexOf("JSONString_") != -1) {
                String type = list.get(ZI_DUAN_LEI_XING).split("_")[1];
                stringBuilder.append(transFieldType(type));
            } else if (list.get(ZI_DUAN_LEI_XING).equals("JsonArray")) {
                String o = oneToUp(list.get(ZI_DUAN_MING));
                stringBuilder.append("List<" + o + "> ");
                obj.add(o);
            } else if (list.get(ZI_DUAN_LEI_XING).equals("JSONObject") || list.get(ZI_DUAN_LEI_XING).equals("Json") || list.get(ZI_DUAN_LEI_XING).equals("JsonObject")) {
                String o = oneToUp(list.get(ZI_DUAN_MING));
                stringBuilder.append(o);
                obj.add(o);
            } else if (list.get(ZI_DUAN_LEI_XING).indexOf("JSONString_") != -1) {
                String type = list.get(ZI_DUAN_LEI_XING).split("_")[1];
                switch (type) {
                    case "LS":
                        stringBuilder.append("List<String>");
                        break;
                    case "LI":
                        stringBuilder.append("List<Integer>");
                    default:
                        break;
                }
            } else {
                stringBuilder.append(javaMap.get(list.get(ZI_DUAN_LEI_XING)));
            }
            stringBuilder.append(" ").append(list.get(ZI_DUAN_MING)).append(";\n");
        }
        if (obj.size() > 0) {
            for (String s : obj) {
                stringBuilder.append("@Setter\n").append("@Getter\npublic static class ").append(s).append("{\n");
                stringBuilder.append("}");
            }
        }
        System.err.println(stringBuilder);
    }

    /**
     * @description: 生成oracle建表sql脚本
     * @param:
     * @return: void
     * @author lixw
     * @date: 2024/11/26 14:28
     */
    private static void generateSqlSh() {
        List<List<String>> lists = ExcelUtil.readXls("C:/Users/13755/Desktop/Bean生成模板.xlsx");
        //List<List<String>> lists = ExcelUtil.readXls("C:/Users/xwli/Desktop/Bean生成模板.xlsx");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < lists.size(); i++) {
            List<String> list = CollUtil.get(lists, i);
            if (list.size() == 0) break;
            if (i == 0) continue;
            int s = 0;
            for (String l : list) {
                if (l.equals(""))
                    s++;
            }
            if (s == list.size()) continue;
            //字段名
            String name = list.get(ZI_DUAN_MING);
            stringBuilder.append(name).append(" ");
            //字段类型
            String type = MapUtil.getStr(sqlMap, list.get(ZI_DUAN_LEI_XING));
            stringBuilder.append(type);
            if (!StringUtils.containsAny(type, "double", "int", "TIMESTAMP", "date")) {
                //字段长度
                stringBuilder.append("(").append(list.get(ZI_DUAN_CHANG_DU)).append(")");
            }
            stringBuilder.append(" ");
            //字段是否为空
            String must = list.get(ZI_DUAN_BI_XU);
            if (StringUtils.isNotBlank(must) && StringUtils.equalsAny(must, "否")) {
                stringBuilder.append("NOT NULL").append(" ");
            }
            //字段描述
            String descrbe = list.get(ZI_DUAN_HAN_YI);
            descrbe = descrbe.replaceAll("\"", "");
            descrbe = descrbe.replaceAll("\n", ",");
            stringBuilder.append("COMMENT '").append(descrbe).append("'").append(",\n");
        }
        System.err.println(stringBuilder);
    }

    /**
     * 首字母转大写
     *
     * @param str
     * @return
     */
    public static String oneToUp(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    public static String transFieldType(String type) {
        switch (type) {
            case "S":
                return "String";
            case "I":
                return "Integer";
            case "LI":
                return "List<Integer>";
            case "LLI":
                return "List<List<Integer>>";
            default:
                break;
        }
        return type;
    }
}
